//
//  SearchResultViewModel.swift
//  shop
//
//  Created by chen on 2025/5/17.
//

import SwiftUI

@MainActor
class SearchResultViewModel: ObservableObject {
    
    static let shared: SearchResultViewModel = SearchResultViewModel()
    
    public init() {
        self.loadMap = self.sortType.reduce(into: [:]) { result, item in
            result[item.type] = false
        }
    }
    
    @Published var sortType: [SortType] = [
        SortType(type: 0, name: "综合"),
        SortType(type: 2, name: "销量"),
        SortType(type: 5, name: "价格高"),
        SortType(type: 6, name: "价格低")
    ]
    
    @Published var searchMap: [Int:[GoodsItem]] = [:]
    
    @Published var loadMap: [Int:Bool] = [:]
    
    public func getSearchMap(keyWord: String, type: Int = 0) {
        Task{
            loadMap[type] = true
            
            let queryParam: [String:Any] = [
                "version": "v2.1.2",
                "pageId": 1,
                "pageSize": 10,
                "keyWords": keyWord,
                "sort": type
            ]
            
            await NetManager.shared.httpGet(.searchGoods, queryParam: queryParam) { (resp: SearchGoodsResp) in
                self.loadMap[type] = false
                self.searchMap[type] = resp.list
            }
        }
    }
}
